import { createMemoryHistory, createRouter } from 'vue-router'

import { userStore } from '@/stores'

import AdminLayout from '@/views/layout/Layout.vue'
import HomeView from '@/views/Home.vue'
import AboutView from '@/views/About.vue'
import LoginView from '@/views/Login.vue'

// 404页面组件
import NotFound from '@/views/error/NotFound.vue';

const routes = [
    { 
        path: '/',
        component: AdminLayout,
        redirect: '/index',
        children:[
            { 
                path: '/index', 
                name: 'index',
                title: '首页',
                icon: 'House',
                component: HomeView
            },
            { 
                path: '/profile', 
                name: 'profile',
                title: '用户信息',
                component: () => import('@/views/userCenter/userInfo.vue')
            },
            { 
                path: '/changePwd', 
                name: 'changePwd',
                title: '修改密码',
                component: () => import('@/views/userCenter/changePwd.vue')
            },
            { 
                path: '/mailbox', 
                name: 'mailbox',
                title: '系统消息',
                component: () => import('@/views/userCenter/mailbox.vue')
            },
            { 
                path: '/logs', 
                name: 'logs',
                title: '日志管理',
                component: () => import('@/views/system/Logs.vue')
            },
            { 
                path: '/users', 
                name: 'users',
                title: '员工管理',
                component: () => import('@/views/system/UserList.vue')
            },
            { 
                path: '/depts', 
                name: 'depts',
                title: '部门管理',
                component: () => import('@/views/system/DeptList.vue')
            },
            // 角色管理	roles		/roles
            { 
                path: '/roles', 
                name: 'roles',
                title: '角色管理',
                component: () => import('@/views/system/RoleList.vue')
            },
            { 
                path: '/menus', 
                name: 'menus',
                title: '菜单管理',
                component: () => import('@/views/system/MenuList.vue')
            },

            // 客户列表	members		/members
            // 套餐管理	shopMeals		/shopMeals
            // 其他设置	other		/other
            { 
                path: '/members', 
                name: 'members',
                title: '会员列表',
                component: () => import('@/views/About.vue')
            },
            
            { 
                path: '/shopMeals', 
                name: 'shopMeals',
                title: '套餐管理',
                component: () => import('@/views/About.vue')
            },
            
            // { 
            //     path: '/other', 
            //     name: 'other',
            //     title: '其他设置',
            //     component: () => import('@/views/About.vue')
            // },
            
        ]
    },
    { path: '/about', component: AboutView },
    { path: '/login', component: LoginView },
    { path: '/404', component: NotFound },
    {
        path: '/:catchAll(.*)', // 捕获所有未匹配的路径
        name: 'NotFound',
        component: NotFound,
    }
]

const router = createRouter({
  history: createMemoryHistory(),
  routes,
})

//挂载路由导航守卫
router.beforeEach((to,from,next)=>{
  
  //如果用户访问的是登录页面，则直接放行
  if(to.path === '/login'){
    next()
  }else{
    //从 sessionStorage 中获取 保存的 token
    const store = userStore()
    const isLogin = store.isLogin
    //如果没有token，强制跳转到登录页面，如果有，则直接放行
    if(!isLogin){
        next('/login')
    }else{
        next()
    }
  }
})


export default router